package com.sbcb.script.rule;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Component
public class RuleFileManager {
    @Autowired
    private RuleLoader ruleLoader;

    @Scheduled(fixedRate = 5000)
    public void scanRules() throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        System.out.println("scan rules");
        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resourcePatternResolver.getResources("classpath:groovy/**/*.groovy");
        List<File> groovyFileList = new ArrayList<>();
        for (Resource resource : resources) {
            File file = resource.getFile();
            groovyFileList.add(file);
            System.out.println(file);
            ruleLoader.addRule(file);
        }
    }
}
